//
//  SZLNearWineFriendsController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/12/15.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLNearWineFriendsController.h"
#import "NearFriendsCell.h"
#import "WineFriendsNetwork.h"
#import "SZLAccountHelper.h"
#import "MBProgressHUD+MJ.h"
#import "WineNewFriendsViewController.h"
#import "EaseUserModel.h"

@interface SZLNearWineFriendsController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong,nonatomic) UITableView *friendsTab;
@property (nonatomic,strong) NSMutableArray *sources;

@end

@implementation SZLNearWineFriendsController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"附近酒友";
    [self setNearFriends];
    [self.view addSubview:self.friendsTab];
}
- (void)setNearFriends
{
    
}
- (void)getNearFriends:(CLLocation *)location
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"token"] = [SZLAccountHelper shareInstance].token;
    params[@"latitude"] = @(location.coordinate.latitude);
    params[@"longitude"] = @(location.coordinate.longitude);
    __WEAK
    [self showWaiter];
    [WineFriendsNetwork getNearWineFriendsWithParame:params response:^(BOOL success, NSString *msg, NSArray *sources) {
        [self hideWaiter];
        if (success) {
            if (sources.count == 0) {
                [weakSelf showNoDataView:self.view.bounds title:@"附近没有酒友"];
            }else{
                [weakSelf.sources addObjectsFromArray:sources];
                [weakSelf.friendsTab reloadData];
            }
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sources.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NearFriendsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([NearFriendsCell class])];
    cell.model = self.sources[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WineNewFriendsViewController *wfVc = [[WineNewFriendsViewController alloc] init];
    FriendsDetailModel *friendModel = self.sources[indexPath.row];
    EaseUserModel *model = [[EaseUserModel alloc]initWithBuddy:friendModel.mobilePhone];
    model.nickname = friendModel.nickName;
    wfVc.userModel = model;
    [self.navigationController pushViewController:wfVc animated:YES];
}
- (UITableView *)friendsTab
{
    if (!_friendsTab) {
        _friendsTab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64)];
        _friendsTab.delegate = self;
        _friendsTab.dataSource = self;
        _friendsTab.rowHeight = 90;
        _friendsTab.tableFooterView = [[UIView alloc]init];
        [_friendsTab registerNib:[UINib nibWithNibName:NSStringFromClass([NearFriendsCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([NearFriendsCell class])];
        [_friendsTab registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    }
    return _friendsTab;
}
- (NSMutableArray *)sources
{
    if (!_sources) {
        _sources = [NSMutableArray array];
    }
    return _sources;
}


@end
